<template>
  <a-modal
    :title="'新增'+addTitle+'的时间调度'"
    :width="'46.875vw'"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <div class="choose-time">
          <ul class="time-list">
            <li class="night allday" style="margin-top: 10px">
              <ol style="padding-left: 20px;" :class="isAllChecked? 'checked':''" @click="allChecked()">
                <span class="scale" style="font-size: 20px">全选</span>
                <span class="unit"> </span>
              </ol>
            </li>
            <li class="morning">
              <div class="moreCheck" @click="checkMoreTime('morning')">
                <img src="~@/assets/img/morning-icon.png" alt="">
                <span style="cursor: pointer;">上午</span>
              </div>
              <ol :class="checkNum[5]===1? 'checked':''" @click="checkTime(5)">
                <span class="scale">5</span>
                <span class="unit">H</span>
              </ol>
              <ol :class="checkNum[6]===1? 'checked':''" @click="checkTime(6)">
                <span class="scale">6</span>
                <span class="unit">H</span>
              </ol>
              <ol :class="checkNum[7]===1? 'checked':''" @click="checkTime(7)">
                <span class="scale">7</span>
                <span class="unit">H</span>
              </ol>
              <ol :class="checkNum[8]===1? 'checked':''" @click="checkTime(8)">
                <span class="scale">8</span>
                <span class="unit">H</span>
              </ol>
              <ol style="padding-left: 20px;" :class="checkNum[9]===1? 'checked':''" @click="checkTime(9)">
                <span class="scale">9</span>
                <span class="unit">H</span>
              </ol>
              <ol style="padding-left: 20px;" :class="checkNum[10]===1? 'checked':''" @click="checkTime(10)">
                <span class="scale">10</span>
                <span class="unit">H</span>
              </ol>
              <ol style="padding-left: 20px;" :class="checkNum[11]===1? 'checked':''" @click="checkTime(11)">
                <span class="scale">11</span>
                <span class="unit">H</span>
              </ol>
            </li>
            <li class="afternoon">
              <div class="moreCheck" @click="checkMoreTime('afternoon')">
                <img src="~@/assets/img/afternoon-icon.png" alt="">
                <span style="cursor: pointer;">下午</span>
              </div>
              <ol :class="checkNum[12]===1? 'checked':''" @click="checkTime(12)">
                <span class="scale">12</span>
                <span class="unit">H</span>
              </ol>
              <ol :class="checkNum[13]===1? 'checked':''" @click="checkTime(13)">
                <span class="scale">13</span>
                <span class="unit">H</span>
              </ol>
              <ol :class="checkNum[14]===1? 'checked':''" @click="checkTime(14)">
                <span class="scale">14</span>
                <span class="unit">H</span>
              </ol>
              <ol :class="checkNum[15]===1? 'checked':''" @click="checkTime(15)">
                <span class="scale">15</span>
                <span class="unit">H</span>
              </ol>
              <ol :class="checkNum[16]===1? 'checked':''" @click="checkTime(16)">
                <span class="scale">16</span>
                <span class="unit">H</span>
              </ol>
              <ol :class="checkNum[17]===1? 'checked':''" @click="checkTime(17)">
                <span class="scale">17</span>
                <span class="unit">H</span>
              </ol>
            </li>
            <li class="evening">
              <div class="moreCheck" @click="checkMoreTime('evening')">
                <img src="~@/assets/img/eveing-icon.png" alt="">
                <span style="cursor: pointer;">晚间</span>
              </div>
              <ol :class="checkNum[18]===1? 'checked':''" @click="checkTime(18)">
                <span class="scale">18</span>
                <span class="unit">H</span>
              </ol>
              <ol :class="checkNum[19]===1? 'checked':''" @click="checkTime(19)">
                <span class="scale">19</span>
                <span class="unit">H</span>
              </ol>
              <ol :class="checkNum[20]===1? 'checked':''" @click="checkTime(20)">
                <span class="scale">20</span>
                <span class="unit">H</span>
              </ol>
              <ol :class="checkNum[21]===1? 'checked':''" @click="checkTime(21)">
                <span class="scale">21</span>
                <span class="unit">H</span>
              </ol>
            </li>
            <li class="night">
              <div class="moreCheck" @click="checkMoreTime('night')">
                <img src="~@/assets/img/night-icon.png" alt="">
                <span style="cursor: pointer;">夜间</span>
              </div>
              <ol style="padding-left: 20px;" :class="checkNum[22]===1? 'checked':''" @click="checkTime(22)">
                <span class="scale">22</span>
                <span class="unit">H</span>
              </ol>
              <ol style="padding-left: 20px;" :class="checkNum[23]===1? 'checked':''" @click="checkTime(23)">
                <span class="scale">23</span>
                <span class="unit">H</span>
              </ol>
              <ol :class="checkNum[0]===1? 'checked':''" @click="checkTime(0)">
                <span class="scale">0</span>
                <span class="unit">H</span>
              </ol>
              <ol :class="checkNum[1]===1? 'checked':''" @click="checkTime(1)">
                <span class="scale">1</span>
                <span class="unit">H</span>
              </ol>
              <ol :class="checkNum[2]===1? 'checked':''" @click="checkTime(2)">
                <span class="scale">2</span>
                <span class="unit">H</span>
              </ol>
              <ol :class="{checked:checkNum[3] === 1}" @click="checkTime(3)">
                <span class="scale">3</span>
                <span class="unit">H</span>
              </ol>
              <ol :class="{checked:checkNum[4] === 1}" @click="checkTime(4)">
                <span class="scale">4</span>
                <span class="unit">H</span>
              </ol>
            </li>
          </ul>
        </div>

        <br><br><br>

        <a-form-item
          label="所属网关"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
          style="margin-top:-30px"
        >
          <a-select
            style="width: 100%"
            placeholder="请选择所属网关"
            v-decorator="['gatewayId', {rules: [{ required: true, message: '请选择所属网关！' }]}]">
            <a-select-option v-for="(item,index) in groupCodeList" :key="index" :value="item.id">{{ item.gatewayName }}
            </a-select-option>
          </a-select>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { add } from '@/api/modular/main/bizgatewayschedule/bizGatewayScheduleManage'
  import { gatewayList } from '@/api/modular/main/bizgateway/bizGatewayManage'
  import { list } from '@/api/modular/main/bizstation/bizStationManage'

  export default {
    props:{
      addTitle:{
        type:String,
        default:'',
        required: true,
      }
    },
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        groupCodeList: {},
        stationTree: [],
        stationId: '',
        checkNum: { 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0, 13: 0, 14: 0, 15: 0, 16: 0, 17: 0, 18: 0, 19: 0, 20: 0, 21: 0, 22: 0, 23: 0 },
        isAllChecked: false
      }
    },
    methods: {
      add (record) {
        const that = this
        that.visible = true

        that.getStationTree()
        that.getStationId(record)
      },
      getStationTree () {
        list().then(res => {
          if (!res.success) {
            return
          }
          this.stationTree = res.data
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          values.timeSchedule = Object.values(this.checkNum).toString().replaceAll(',', '')
          if (!errors) {
            add(values).then((res) => {
              this.confirmLoading = false
              if (res.success) {
                this.$message.success('新增成功')
                this.handleCancel()
                this.$emit('ok', values)
              } else {
                this.$message.info('新增失败：' + res.message)
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      getStationId (value) {
        const param = {
          stationId: value
        }
        gatewayList(param).then(res => {
          this.groupCodeList = res.data
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      },
      checkTime (index) {
        if (this.checkNum[index] === 1) {
          this.checkNum[index] = 0
        } else {
          this.checkNum[index] = 1
        }
        let count = 0
        for (let i = 0; i < 24; i++) {
          if (this.checkNum[i] === 1) {
            count++
          }
        }
        if (count === 24) {
          this.isAllChecked = true
        } else {
          this.isAllChecked = false
        }
      },
      checkMoreTime (time) {
        if(time === 'morning'){
          if (this.checkNum[5] === 1 && this.checkNum[6] === 1 && this.checkNum[7] === 1 && this.checkNum[8] === 1 && this.checkNum[9] === 1 && this.checkNum[10] === 1 && this.checkNum[11] === 1) {
            this.checkNum[5] = 0
            this.checkNum[6] = 0
            this.checkNum[7] = 0
            this.checkNum[8] = 0
            this.checkNum[9] = 0
            this.checkNum[10] = 0
            this.checkNum[11] = 0
          }else{
            this.checkNum[5] = 1
            this.checkNum[6] = 1
            this.checkNum[7] = 1
            this.checkNum[8] = 1
            this.checkNum[9] = 1
            this.checkNum[10] = 1
            this.checkNum[11] = 1
          }
        }else if(time === 'afternoon'){
          if (this.checkNum[12] === 1 && this.checkNum[13] === 1 && this.checkNum[14] === 1 && this.checkNum[15] === 1 && this.checkNum[16] === 1 && this.checkNum[17] === 1) {
            this.checkNum[12] = 0
            this.checkNum[13] = 0
            this.checkNum[14] = 0
            this.checkNum[15] = 0
            this.checkNum[16] = 0
            this.checkNum[17] = 0
          }else{
            this.checkNum[12] = 1
            this.checkNum[13] = 1
            this.checkNum[14] = 1
            this.checkNum[15] = 1
            this.checkNum[16] = 1
            this.checkNum[17] = 1
          }
        }else if(time === 'evening'){
          if (this.checkNum[18] === 1 && this.checkNum[19] === 1 && this.checkNum[20] === 1 && this.checkNum[21] === 1) {
            this.checkNum[18] = 0
            this.checkNum[19] = 0
            this.checkNum[20] = 0
            this.checkNum[21] = 0
          }else{
            this.checkNum[18] = 1
            this.checkNum[19] = 1
            this.checkNum[20] = 1
            this.checkNum[21] = 1
          }
        }else if(time === 'night'){
          if (this.checkNum[22] === 1 && this.checkNum[23] === 1 && this.checkNum[0] === 1 && this.checkNum[1] === 1 && this.checkNum[2] === 1 && this.checkNum[3] === 1 && this.checkNum[4] === 1) {
            this.checkNum[22] = 0
            this.checkNum[23] = 0
            this.checkNum[0] = 0
            this.checkNum[1] = 0
            this.checkNum[2] = 0
            this.checkNum[3] = 0
            this.checkNum[4] = 0
          }else{
            this.checkNum[22] = 1
            this.checkNum[23] = 1
            this.checkNum[0] = 1
            this.checkNum[1] = 1
            this.checkNum[2] = 1
            this.checkNum[3] = 1
            this.checkNum[4] = 1
          }
        }
        let count = 0
        for (let i = 0; i < 24; i++) {
          if (this.checkNum[i] === 1) {
            count++
          }
        }
        if (count === 24) {
          this.isAllChecked = true
        } else {
          this.isAllChecked = false
        }
      },
      allChecked () {
        if (!this.isAllChecked) {
          for (let i = 0; i < 24; i++) {
            this.checkNum[i] = 1
          }
          this.isAllChecked = true
        } else {
          for (let i = 0; i < 24; i++) {
            this.checkNum[i] = 0
          }
          this.isAllChecked = false
        }
      },
      getClass (index) {
        if (this.checkNum[index] === 1) { return 'checked' }
      }
    }
  }
</script>
<style scoped>
  @import '../~@/assets/scss/fonts.css';
  .ant-checkbox-wrapper {
    color: #fff;
  }
  .ant-modal-content .ant-modal-header .ant-modal-title{
    /*color: #19ECFF;*/
    font-size: 20px;
    padding-left: 30px;
  }
  .ant-modal-content .ant-modal-header{
    background-color: #0D5B6A;
  }
  .ant-modal-body{
    background: linear-gradient(to bottom,#0B414B,#10424A);
  }
  .choose-time{
    padding-left: 20px;
    font-family: 'DIN';
  }

  .ant-form .choose-time .time-list .allCheck{
    height: 0px;
    line-height: 44px;
    margin-bottom: 10px;
    margin-top: 20px;
    text-align: right;
  }
  .ant-form .choose-time .time-list .morning{
    height: 44px;
    line-height: 44px;
    margin-bottom: 10px;
    margin-top: 20px;
  }
  .ant-form .choose-time .time-list .morning img{
    float: left;
    height: 23px;
    width: 26px;
    margin-top: 10px;
    margin-right: 18px;
    margin-left: 5px;
    padding-left: 1px;
  }
  .ant-form .choose-time .time-list .morning span{
    color: #F3F3F3;
    float: left;
    margin-right: 40px;
    font-size: 17px;
  }
  .ant-form .choose-time .time-list .morning ol{
    float: left;
    color: #F3F3F3;
    width: 84px;
    height: 44px;
    line-height: 44px;
    background-image: url('~@/assets/img/timebg.png');
    background-size: 100% 100%;
    background-repeat: no-repeat;
    margin-right: 10px;
    padding-left: 30px;
    text-align: center;
    cursor: pointer;
    opacity: 0.5;
  }
  .ant-form .choose-time .time-list .morning .checked{
    background-image: url('~@/assets/img/timeckbg.png') !important;
    opacity: 1;
    color: #19ECFF !important;
  }
  .ant-form .choose-time .time-list .morning ol span{
    margin: 0;
  }
  .ant-form .choose-time .time-list .morning ol .scale{
    font-size: 24px;
  }
  .ant-form .choose-time .time-list .morning ol .unit{
    font-size: 14px;
    transform: translateY(4px) translateX(2px);
  }
  .ant-form .choose-time .time-list .morning .checked .scale,
  .ant-form .choose-time .time-list .morning .checked .unit{
    opacity: 1 !important;
    color: #19ECFF !important;
  }

  .ant-form .choose-time .time-list .afternoon{
    height: 44px;
    line-height: 44px;
    margin-bottom: 40px;
  }
  .ant-form .choose-time .time-list .afternoon img{
    float: left;
    height: 25px;
    width: 26px;
    margin-top: 10px;
    margin-right: 18px;
    margin-left: 5px;
    padding-left: 1px;
  }
  .ant-form .choose-time .time-list .afternoon span{
    color: #F3F3F3;
    float: left;
    margin-right: 40px;
    font-size: 17px;
  }
  .ant-form .choose-time .time-list .afternoon ol{
    float: left;
    color: #F3F3F3;
    width: 84px;
    height: 44px;
    line-height: 44px;
    background-image: url('~@/assets/img/timebg.png');
    background-size: 100% 100%;
    background-repeat: no-repeat;
    margin-right: 10px;
    padding-left: 20px;
    text-align: center;
    cursor: pointer;
    opacity: 0.5;
  }
  .ant-form .choose-time .time-list .afternoon .checked{
    background-image: url('~@/assets/img/timeckbg.png') !important;
    opacity: 1;
    color: #19ECFF !important;
  }
  .ant-form .choose-time .time-list .afternoon ol span{
    margin: 0;
  }
  .ant-form .choose-time .time-list .afternoon ol .scale{
    font-size: 24px;
  }
  .ant-form .choose-time .time-list .afternoon ol .unit{
    font-size: 14px;
    transform: translateY(4px) translateX(2px);
  }
  .ant-form .choose-time .time-list .afternoon .checked .scale,
  .ant-form .choose-time .time-list .afternoon .checked .unit{
    opacity: 1 !important;
    color: #19ECFF !important;
  }

  .ant-form .choose-time .time-list .evening{
    height: 44px;
    line-height: 44px;
    margin-bottom: 10px;
  }
  .ant-form .choose-time .time-list .evening img{
    float: left;
    height: 25px;
    width: 26px;
    margin-top: 10px;
    margin-right: 18px;
    margin-left: 5px;
  }
  .ant-form .choose-time .time-list .evening span{
    color: #F3F3F3;
    float: left;
    margin-right: 40px;
    font-size: 17px;
  }
  .ant-form .choose-time .time-list .evening ol{
    float: left;
    color: #F3F3F3;
    width: 84px;
    height: 44px;
    line-height: 44px;
    background-image: url('~@/assets/img/timebg.png');
    background-size: 100% 100%;
    background-repeat: no-repeat;
    margin-right: 10px;
    padding-left: 20px;
    text-align: center;
    cursor: pointer;
    opacity: 0.5;
  }
  .ant-form .choose-time .time-list .evening .checked{
    background-image: url('~@/assets/img/timeckbg.png') !important;
    opacity: 1 !important;
    color: #19ECFF !important;
  }
  .ant-form .choose-time .time-list .evening ol span{
    margin: 0;
    opacity: 1!important;
  }
  .ant-form .choose-time .time-list .evening ol .scale{
    font-size: 24px;
  }
  .ant-form .choose-time .time-list .evening ol .unit{
    font-size: 14px;
    transform: translateY(4px) translateX(2px);
  }
  .ant-form .choose-time .time-list .evening .checked .scale,
  .ant-form .choose-time .time-list .evening .checked .unit{
    opacity: 1 !important;
    color: #19ECFF !important;
  }

  .ant-form .choose-time .time-list .night{
    height: 44px;
    line-height: 44px;
  }
  .ant-form .choose-time .time-list .night img{
    float: left;
    width: 26px;
    height: 26px;
    margin-top: 10px;
    margin-right: 18px;
    margin-left: 5px;
    padding: 0 4px;
  }
  .ant-form .choose-time .time-list .night span{
    color: #F3F3F3;
    float: left;
    margin-right: 40px;
    font-size: 17px;
  }
  .ant-form .choose-time .time-list .night ol{
    font-weight: normal;
    float: left;
    color: #F3F3F3;
    width: 84px;
    height: 44px;
    line-height: 44px;
    background-image: url('~@/assets/img/timebg.png');
    background-size: 100% 100%;
    background-repeat: no-repeat;
    margin-right: 10px;
    padding-left: 30px;
    text-align: center;
    cursor: pointer;
    opacity: 0.5;
  }
  .ant-form .choose-time .time-list .night .checked{
    background-image: url('~@/assets/img/timeckbg.png') !important;
    opacity: 1 !important;
    color: #19ECFF !important;
  }
  .ant-form .choose-time .time-list .night ol span{
    margin: 0;
    opacity: 1!important;
  }
  .ant-form .choose-time .time-list .night ol .scale{
    font-size: 24px;
  }
  .ant-form .choose-time .time-list .night ol .unit{
    font-size: 14px;
    transform: translateY(4px) translateX(2px);
  }
  .ant-form .choose-time .time-list .night .checked .scale,
  .ant-form .choose-time .time-list .night .checked .unit{
    opacity: 1 !important;
    color: #19ECFF !important;
  }
  .ant-form .choose-time .time-list .allday{
    margin-left: 123px;
  }
  .ant-form .choose-time .time-list .allday ol{
    float: left;
    color: #F3F3F3;
    width: 160px;
    height: 52px;
    line-height: 52px;
    padding-left: 60px !important;
    text-align: center;
  }

  @media (min-width: 576px){
    .ant-col-sm-5 {
      display: block;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
      width: 12.833333%;
    }
    .ant-col-sm-15 {
      display: block;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
      width: 76.5%;
      margin-left: 22px;
    }
  }
</style>
